package com.kalix.training.live.biz;


import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.api.persistence.JsonStatus;
import com.kalix.framework.core.impl.biz.ShiroGenericBizServiceImpl;
import com.kalix.training.live.api.biz.ILiveRoomBeanService;
import com.kalix.training.live.api.dao.ILiveRoomBeanDao;
import com.kalix.training.live.entities.LiveRoomBean;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class LiveRoomBeanServiceImpl extends ShiroGenericBizServiceImpl<ILiveRoomBeanDao, LiveRoomBean>
        implements ILiveRoomBeanService {

    /**
     * @Author yyp
     * @Description 创建直播间
     * @Date 8:56 2019/2/16
     * @update 10:36 2019/04/15
     **/
    public JsonStatus addLiveRoom(LiveRoomBean liveroom){
//        String sql = "INSERT INTO \"public\".\"mooc_live_room\" (id,userid,examine,liveid,livename) VALUES (3,2,'开播',12321,'')";
        LiveRoomBean liveRoomBean = new LiveRoomBean();
        liveRoomBean.setLiveId((long)((Math.random()*9+1)*100000));
        liveRoomBean.setExamine("开播");
        liveRoomBean.setNum(0L);
        liveRoomBean.setUserId(liveroom.getUserId());
        liveRoomBean.setLiveName(liveroom.getLiveName());
        liveRoomBean.setLiveUrl("rtmp://192.168.31.133:1935/hls/"+ UUID.randomUUID().toString());
        LiveRoomBean liveRoomBean1 = dao.save(liveRoomBean);
        JsonStatus jsonStatus = new JsonStatus();
        if (liveRoomBean1 != null) {
            jsonStatus.setSuccess(true);
            jsonStatus.setMsg("保存成功！");
        } else {
            jsonStatus.setFailure(true);
            jsonStatus.setMsg("保存失败！");
        }
        return jsonStatus;
    }

    /**
     * @Author lwj
     * @Description 关闭直播间
     * @Date 8:56 2019/2/16
     **/
    public String closeLiveRoom(Long userId){
        String sql = "UPDATE \"public\".\"mooc_live_room\"" + "SET examine = '闭播'" + "WHERE" + "\tuserid = " +userId;
        int num = dao.updateNativeQuery(sql);
        return "已关闭直播间";
    }

    /**
     * @Author lwj
     * @Description 设定直播间位置
     * @Date 14:15 2019/2/22
     **/
    public void beforeSaveEntity(LiveRoomBean entity, JsonStatus status){
        if(entity.getNum() != null){
            Long num = entity.getNum();
            List<LiveRoomBean> liveRoomBeans = dao.getAll();
            for (int i = 0; i < liveRoomBeans.size(); i++) {
                if(liveRoomBeans.get(i).getNum() == num){
                    dao.updateNativeQuery("UPDATE \"public\".\"mooc_live_room\" SET num = '0' WHERE id = "+liveRoomBeans.get(i).getId());
                    dao.updateNativeQuery("UPDATE \"public\".\"mooc_live\" SET num = '0' WHERE liveid = 123"+liveRoomBeans.get(i).getLiveId());

                }
            }
        }
    }

    /**
     * @Author lwj
     * @Description 修改直播间位置
     * @Date 14:17 2019/2/22
     **/
    public void beforeUpdateEntity(LiveRoomBean entity, JsonStatus status){
        if (entity.getNum() != null) {
            Long num = entity.getNum();
            List<LiveRoomBean> liveRoomBeans = dao.getAll();
            for (int i = 0; i < liveRoomBeans.size(); i++) {
                if(liveRoomBeans.get(i).getNum() == num){
                    dao.updateNativeQuery("UPDATE \"public\".\"mooc_live_room\" SET num = '0' WHERE id = "+liveRoomBeans.get(i).getId());
                    dao.updateNativeQuery("UPDATE \"public\".\"mooc_live\" SET num = '0' WHERE liveid = 123"+liveRoomBeans.get(i).getLiveId());
                }
            }
        }
    }
}
